#include <bits/stdc++.h>
using namespace std;

const int N = 505, K = 18;
int h, w, f[N][N], q;

inline int id(int x, int y) { return (x - 1) * w + y; }

struct edge {
  int u, v, w;
  bool operator<(const edge &oth) const { return w > oth.w; }
} e[N * N * 2];

struct disjoint_sets_union {
  int fa[N * N];
  void init() {
    for (int i = 1; i <= h * w; i++)
      fa[i] = i;
  }
  int find(int p) { return fa[p] == p ? p : fa[p] = find(fa[p]); }
} dsu;

vector<pair<int, int>> G[N * N];
int par[N * N][K], minw[N * N][K], dep[N * N];

void build(int u) {
  for (int k = 1; k < K; k++) {
    par[u][k] = par[par[u][k - 1]][k - 1];
    minw[u][k] = min(minw[u][k - 1], minw[par[u][k - 1]][k - 1]);
  }
  for (auto [v, w] : G[u]) {
    if (v == par[u][0])
      continue;
    par[v][0] = u;
    minw[v][0] = w;
    dep[v] = dep[u] + 1;
    build(v);
  }
}

int query(int u, int v) {
  int ans = 0x3f3f3f3f;
  if (dep[u] < dep[v])
    swap(u, v);
  for (int k = K - 1; ~k; k--) {
    if (dep[par[u][k]] >= dep[v]) {
      ans = min(ans, minw[u][k]);
      u = par[u][k];
    }
  }
  if (u == v)
    return ans;
  for (int k = K - 1; ~k; k--) {
    if (par[u][k] == par[v][k])
      continue;
    ans = min(ans, minw[u][k]);
    ans = min(ans, minw[v][k]);
    u = par[u][k];
    v = par[v][k];
  }
  ans = min(ans, minw[u][0]);
  ans = min(ans, minw[v][0]);
  return ans;
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      scanf("%d", &f[i][j]);

  int cnt = 0;
  for (int i = 1; i < h; i++) {
    for (int j = 1; j <= w; j++) {
      e[++cnt] = {id(i, j), id(i + 1, j), min(f[i][j], f[i + 1][j])};
    }
  }

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j < w; j++) {
      e[++cnt] = {id(i, j), id(i, j + 1), min(f[i][j], f[i][j + 1])};
    }
  }

  sort(e + 1, e + cnt + 1);
  dsu.init();

  for (int i = 1; i <= cnt; i++) {
    int a = dsu.find(e[i].u);
    int b = dsu.find(e[i].v);
    if (a != b) {
      dsu.fa[a] = b;
      G[a].emplace_back(b, e[i].w);
      G[b].emplace_back(a, e[i].w);
    }
  }

  memset(minw, 0x3f, sizeof(minw));
  dep[1] = 1;
  build(1);

  scanf("%d", &q);
  while (q--) {
    int a, b, y, c, d, z;
    scanf("%d %d %d %d %d %d", &a, &b, &y, &c, &d, &z);
    int L = query(id(a, b), id(c, d));
    L = min({L, y, z});
    printf("%d\n", y - L + z - L);
  }
  return 0;
}